package demo01;

import java.sql.*;

public class JDBCTest01 {
    public static void main(String[] args) {
        //URL地址
        String url = "jdbc:mysql://localhost:3306/zyg_pm1";
        String username = "root";
        String password = "Fang060825";
        try {
            //1.加载驱动程序
            Class.forName("com.mysql.jdbc.Driver");
            //2.获得数据库链接
            Connection conn = DriverManager.getConnection(url, username, password);
            if(conn!=null){
                System.out.println("数据库连接成功");
            }else {
                System.out.println("数据库连接失败");
            }
            //3.通过数据库的连接操作数据库，实现增删改查
            Statement stmt = conn.createStatement();
            String sql = "select * from user";
            ResultSet rs=stmt.executeQuery(sql);
            while(rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("username");
                String word = rs.getString("password");
                System.out.println(id+" "+name+" "+ word);
            }
            //4.释放内存
              if(rs!=null){
                  rs.close();
              }
              if(stmt!=null){
                  stmt.close();
              }
              if(conn!=null){
                  conn.close();
              }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}
